package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.constant.MessageConstant;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Member;
import com.itheima.health.service.MemberService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * @author ：24K
 * @date ：2020/12/5 21:13
 */
@RestController
@RequestMapping("member")
public class MemberController {

    @Reference
    private MemberService memberService;

    /**
     * 分页查询用户列表
     * @param queryPageBean
     * @return
     */
    @PostMapping("findPage")
    @PreAuthorize("hasAuthority('USER_QUERY')")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult<Member> pageResult = memberService.findPage(queryPageBean);
        return new Result(true, "查询会员成功",pageResult);
    }

    /**
     * 添加新会员
     * @param member
     * @return
     */
    @PostMapping("add")
    @PreAuthorize("hasAuthority('USER_ADD')")
    public Result add(@RequestBody Member member) {
        member.setRemark("电脑注册");
        memberService.add(member);
        return new Result(true, MessageConstant.ADD_MEMBER_SUCCESS);
    }

    /**
     * 会员信息的回显
     * @param id
     * @return
     */
    @GetMapping("findById")
    @PreAuthorize("hasAuthority('USER_QUERY')")
    public Result findById(int id) {
        Member member = memberService.findById(id);
        return new Result(true, "获取会员信息成功", member);
    }

    /**
     * 删除会员
     * @param id
     * @return
     */
    @PostMapping("deleteById")
    @PreAuthorize("hasAuthority('USER_DELETE')")
    public Result deleteById(int id) {
        memberService.deleteById(id);
        return new Result(true, "删除会员成功");
    }

    /**
     * 修改会员
     * @param member
     * @return
     */
    @PostMapping("update")
    @PreAuthorize("hasAuthority('USER_EDIT')")
    public Result update(@RequestBody Member member) {
        memberService.update(member);
        return new Result(true, "修改会员信息成功");
    }
}